package com.ruoyi.bole.wechatSubscribe.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.bole.wechatSubscribe.service.WxSubscribeService;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.http.HttpUtils;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class WxSubscribeServiceImpl implements WxSubscribeService {
    // 微信小程序配置
    private static final String APPID = "wx1ce0d323ecff9862";
    private static final String SECRET = "981546ad863f53d6aceba691bb76b0b8";
    
    // 订阅消息模板ID
    private static final String ORDER_TEMPLATE_ID = "nOBpWMAyW_l91hC1YmG_9Bme8zgEYBvEl8d7e0Con7w";
    
    // 微信接口URL
    private static final String ACCESS_TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s";
    private static final String SUBSCRIBE_MESSAGE_URL = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=%s";

    @Override
    public AjaxResult sendSubscribeMessage(String openid, String templateId, Map<String, String> data) {
        try {
            // 验证openid
            if (openid == null || openid.isEmpty()) {
                return AjaxResult.error("openid不能为空");
            }
            
            // 验证templateId
            if (templateId == null || templateId.isEmpty()) {
                return AjaxResult.error("templateId不能为空");
            }
            
            // 验证data
            if (data == null || data.isEmpty()) {
                return AjaxResult.error("data不能为空");
            }
            
            // 获取access_token
            String accessTokenUrl = String.format(ACCESS_TOKEN_URL, APPID, SECRET);
            JSONObject tokenResult = JSONObject.parseObject(HttpUtils.sendGet(accessTokenUrl));
            String accessToken = tokenResult.getString("access_token");
            
            if (accessToken == null) {
                return AjaxResult.error("获取access_token失败: " + tokenResult.getString("errmsg"));
            }
            
            // 构建消息体
            JSONObject message = new JSONObject();
            message.put("touser", openid);
            message.put("template_id", templateId);
            
            JSONObject dataJson = new JSONObject();
            for (Map.Entry<String, String> entry : data.entrySet()) {
                JSONObject value = new JSONObject();
                value.put("value", entry.getValue());
                dataJson.put(entry.getKey(), value);
            }
            message.put("data", dataJson);
            
            // 发送订阅消息
            String sendUrl = String.format(SUBSCRIBE_MESSAGE_URL, accessToken);
            String result = HttpUtils.sendPost(sendUrl, message.toString());
            JSONObject sendResult = JSONObject.parseObject(result);
            
            if (sendResult.getIntValue("errcode") == 0) {
                System.out.println("订阅消息发送成功");
                return AjaxResult.success("订阅消息发送成功");
            } else {
                System.out.println("订阅消息发送失败: " + sendResult.getString("errmsg"));
                return AjaxResult.error("订阅消息发送失败: " + sendResult.getString("errmsg"));
            }
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("订阅消息发送异常: " + e.getMessage());
            return AjaxResult.error("订阅消息发送异常: " + e.getMessage());
        }
    }
}